#include "s3c2440_soc.h"
#include "uart.h"
#include "nand.h"

int strcmp(const char * cs,const char * ct)
{
	register signed char __res;

	while (1) {
		if ((__res = *cs - *ct++) != 0 || !*cs++)
		break;
	}

	return __res;
}

void access_test_init(int val)
{
	BANKCON0 = (val << 8);
}

void sdram_init(void)
{
	BWSCON = 0x22000000;

	BANKCON6 = 0x18001;
	BANKCON7 = 0x18001;

	REFRESH = 0x008004f5;

	BANKSIZE = 0xb1;

	MRSRB6 = 0x20;
	MRSRB7 = 0x20;
}

int sdram_init_test(void)
{
	int i = 0;
	volatile long * p = (volatile long *)0x32f80000;

	for(i=0; i<10; i++)
	{
		p[i] = 0x55;
		return 1;
	}

	for(i=0; i<10; i++)
	{
		if(p[i] != 0x55)
		{
			puts("error 0x55\r\n");
			return -1;
		}
	}
	return 0;
}

int isBootFromNorFlash(void)
{
	volatile unsigned int *p = (volatile unsigned int *)0;
	unsigned int val = *p;
	*p = 0x12345678;
	if(*p == 0x12345678)
	{
		*p = val;
		return 0;
	}
	else
	{
		return 1;
	}
}

#if 1
extern int __code_start, __bss_start, __bss_end;
void copycode2sdram(void)
{
	/*	注意：拷贝代码是从0开始拷贝的！！！    所以src是指向0
		从lds中获得code要拷贝到SDRAM内存的           ----起始地址----与----结束地址

		就是先从lds获取__code_start, __bss_start，
		然后从0地址复制数据到__code_start
	*/
	//声明下外部变量
	//extern int __code_start, __bss_start;
	volatile unsigned int * src  = (volatile unsigned int *)0;
	volatile unsigned int * dest = (volatile unsigned int *)&__code_start;
	volatile unsigned int * end  = (volatile unsigned int *)&__bss_start;

	int len = (int)(&__bss_start) - (int)(&__code_start);
	len *= 4; //如果是按指针减操作，每个指针就对应4

	if(isBootFromNorFlash())
	{
		while(dest <= end)	//(dest < end)????
		{
			*dest++ = *src++;
		}
	}
	else
	{
		nand_init();
		nand_read(src, dest, len);
	}
}

void clear_bss(void)
{
	//从lds中获取__bss_start, __bss_end，然后全部写0
	//extern int __bss_start, __bss_end;
	volatile unsigned int * bss_start = (volatile unsigned int *)&__bss_start;
	volatile unsigned int * bss_end   = (volatile unsigned int *)&__bss_end;

	while(bss_start <= bss_end)	//(bss_start < bss_end)????
	{
		*bss_start++ = 0;
	}
}
#endif

#if 0
/* the literal pools origin */
TQ2440里lowlenel_init.S
SMRDATA:
    .word (0+(B1_BWSCON<<4)+(B2_BWSCON<<8)+(B3_BWSCON<<12)+(B4_BWSCON<<16)+(B5_BWSCON<<20)+(B6_BWSCON<<24)+(B7_BWSCON<<28))
    .word ((B0_Tacs<<13)+(B0_Tcos<<11)+(B0_Tacc<<8)+(B0_Tcoh<<6)+(B0_Tah<<4)+(B0_Tacp<<2)+(B0_PMC))
    .word ((B1_Tacs<<13)+(B1_Tcos<<11)+(B1_Tacc<<8)+(B1_Tcoh<<6)+(B1_Tah<<4)+(B1_Tacp<<2)+(B1_PMC))
    .word ((B2_Tacs<<13)+(B2_Tcos<<11)+(B2_Tacc<<8)+(B2_Tcoh<<6)+(B2_Tah<<4)+(B2_Tacp<<2)+(B2_PMC))
    .word ((B3_Tacs<<13)+(B3_Tcos<<11)+(B3_Tacc<<8)+(B3_Tcoh<<6)+(B3_Tah<<4)+(B3_Tacp<<2)+(B3_PMC))
    .word ((B4_Tacs<<13)+(B4_Tcos<<11)+(B4_Tacc<<8)+(B4_Tcoh<<6)+(B4_Tah<<4)+(B4_Tacp<<2)+(B4_PMC))
    .word ((B5_Tacs<<13)+(B5_Tcos<<11)+(B5_Tacc<<8)+(B5_Tcoh<<6)+(B5_Tah<<4)+(B5_Tacp<<2)+(B5_PMC))
    .word ((B6_MT<<15)+(B6_Trcd<<2)+(B6_SCAN))
    .word ((B7_MT<<15)+(B7_Trcd<<2)+(B7_SCAN))
/*    .word ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Trc<<18)+(Tchr<<16)+REFCNT)	*/	/* HJ_del 100MHz */
/*    .word 0x32		*/							/* HJ_del 100MHz */
/*    .word 0x30		*/							/* HJ_del 100MHz */
/*    .word 0x30		*/							/* HJ_del 100MHz */
/*    .word ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Tsrc<<18)+REFCNT)	*/		/* HJ_del 133MHz */
/*    .word 0xB2		*/							/* HJ_del 133MHz */
/*    .word 0x20		*/							/* HJ_del 133MHz */
/*    .word 0x20		*/							/* HJ_del 133MHz */
//#if CONFIG_133MHZ_SDRAM
    .word ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Tsrc<<18)+REFCNT)
    .word 0xB2
    .word 0xB0
    .word 0xB0
//#else
    .word ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Trc<<18)+(Tchr<<16)+REFCNT)
    .word 0x32   --- 2表示128M SDRM 故这里也改为1，并禁用了burst模式，不然就是0xb1
    .word 0x30
    .word 0x30   ---- 0x20表示2个clock，0x30表示3个clock，其实这两个都可以
//#endif
#endif


